<?php
require_once './include/common.inc.php';
require_once('install/install.sql.php');
if(substr_count($_SERVER['SCRIPT_NAME'], '/') < 2) exit('<a href="http://www.akcms.com/manual/197.htm" target="_blank">please move akcms to a folder before install it.</a>');
if(file_exists(AK_ROOT.'configs/install.lock')) exit('<a href="http://www.akcms.com/manual/198.htm" target="_blank">akcms installed!<br>if you want to reinstall it please remove /configs/install.lock first!<br><b style="color:red">Notice:reinstall will destory all data!</b></a>');
$language = 'english';
if(!empty($get_language)) $language = $get_language;
if(!empty($post_language)) $language = $post_language;
$nodb = 1;
require_once AK_ROOT.'include/admin.inc.php';
if(!empty($post_charset_tag)) $charset = $post_charset_tag;
$array_files = array(
	'..',
	'cache',
	'templates',
	'cache/templates',
	'configs',
);
$message = '';
foreach($array_files as $file) {
	if(!is_writable($file)) $message .= '"'.$file.'"'.$lan['isunwritable'].'<br>';
}
if(!empty($message)) aexit('<a href="http://www.akcms.com/manual/196.htm" target="_blank">'.$message.'</a>');
if(isset($post_installsubmit)) {
	if(!preg_match('/^[0-9a-zA-Z]+$/i', $post_tablepre_tag)) {
		adminmsg($lan['tablepreerror'], 'back', 3, 1);
	} else {
		$tablepre = $post_tablepre_tag;
	}
	$codekey = random(6);
	if($post_dbtype == 'mysql') {
		$__dbtype = 'mysql';
		if(!$db_conn = @mysql_connect($post_dbhost_tag, $post_dbuser_tag, $post_dbpw_tag)) {
			adminmsg($lan['connecterror'], 'back', 3, 1);
		} else {
			if(!mysql_select_db($post_dbname_tag, $db_conn)) {
				if($post_charset_tag == 'utf8') {
					$mysql_charset = ' DEFAULT CHARACTER SET utf8 COLLATE utf8_bin';
				} elseif($post_charset_tag == 'gbk') {
					$mysql_charset = ' DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci';
				} elseif($post_charset_tag == 'english') {
					$mysql_charset = ' DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci';
				}
				$createdatabasesql = 'CREATE DATABASE '.$post_dbname_tag;
				if(mysql_get_server_info() > '4.1') $createdatabasesql = $createdatabasesql.$mysql_charset;
				mysql_query($createdatabasesql, $db_conn);
			}
			mysql_close($db_conn);
			$str_config = '';
			$array_config = array('dbhost', 'dbuser', 'dbpw', 'dbname', 'template_path', 'codekey', 'tablepre', 'cookiepre', 'charset', 'timedifference');
			foreach($array_config as $config) {
				$post = 'post_'.$config.'_tag';
				$str_config .= '$'.$config.' = '.'\''.(isset($$post) ? $$post : $$config).'\''.";\n";
				$$config = isset($$post) ? $$post : $$config;
			}
			$str_config .= '$ifdebug = 0;'."\n";
			$str_config = "<?php\n".$str_config."?>";
			if($fp = @fopen('./configs/config.inc.php', 'w')) {
				fwrite($fp, $str_config);
				fclose($fp);
			}
			$db = db();
			foreach($createtablesql as $key => $value) {
				$value['charset'] = $post_charset_tag;
				$createtablesql = table2mysql($tablepre.'_'.$key, $value);
				$_sqls = explode(";\n", $createtablesql);
				foreach($_sqls as $_sql) {
					mysql_query($_sql);
				}
			}
		}
	} elseif($post_dbtype == 'sqlite') {
		$__dbtype = 'sqlite';
		if(!$db_conn = sqlite_open($post_dbname_tag)) {
			adminmsg($lan['connecterror'], 'back', 3, 1);
		} else {
			sqlite_close($db_conn);
			$dbname = $post_dbname_tag;
			$tablepre = $post_tablepre_tag;
			$str_config = '';
			$array_config = array('dbname', 'template_path', 'codekey', 'tablepre', 'cookiepre', 'charset', 'timedifference');
			foreach($array_config as $config) {
				$post = 'post_'.$config.'_tag';
				$str_config .= '$'.$config.' = '.'\''.(isset($$post) ? $$post : $$config).'\''.";\n";
			}
			$str_config .= '$ifdebug = 0;'."\n";
			$str_config = "<?php\n".$str_config."?>";
			if($fp = @fopen('./configs/config.inc.php', 'w')) {
				fwrite($fp, $str_config);
				fclose($fp);
			}
			require_once AK_ROOT.'include/db_sqlite.class.php';
			$db = new dbstuff2;
			$db->open(AK_ROOT.$dbname);
			foreach($createtablesql as $key => $value) {
				$value['charset'] = $post_charset_tag;
				$createtablesql = table2sqlite($tablepre.'_'.$key, $value);
				runquery($createtablesql);
			}
		}
	}
	foreach($insertsql as $key => $value) {
		$tablename = str_replace('ak_', $tablepre.'_', $value['tablename']);
		if($value['tablename'] == 'settings' && $value['value']['variable'] == 'language') $value['value']['value'] = $language;
		$db->insert($tablename, $value['value']);
	}
	ak_touch(AK_ROOT.'configs/install.lock');
	adminmsg($lan['installsuccess'], 'login.php?first=1');
} elseif(!isset($_GET['language'])) {
	displaytemplate('chooselanguage.htm');
} elseif(!isset($_GET['dbtype'])) {
	$mysqlsupport = 1;
	$sqlitesupport = 1;
	if(!function_exists('mysql_connect')) $mysqlsupport = 0;
	if(!function_exists('sqlite_open')) $sqlitesupport = 0;
	$smarty->assign('sqlitesupport', $sqlitesupport);
	$smarty->assign('mysqlsupport', $mysqlsupport);
	displaytemplate('choosedb.htm');
} else {
	$smarty->assign('servertime', date('Y-m-d H:i:s', time()));
	$smarty->assign('timedifference', $timedifference);
	if($language == 'chinese') {
		$languagecharset = array(
			'key' => 'gbk',
			'value' => 'GBK'
		);
	} elseif($language == 'english') {
		$languagecharset = array(
			'key' => 'english',
			'value' => 'English'
		);
	}
	$smarty->assign('languagecharset', $languagecharset);
	if($get_dbtype == 'mysql') {
		displaytemplate('install_mysql.htm');
	} else {
		$dbname = 'data/'.random(6).'.db.php';
		$smarty->assign('dbname', $dbname);
		displaytemplate('install_sqlite.htm');
	}
}
?>